package com.ylx.service;

import com.ylx.mapper.OrderMapper;
import com.ylx.pojo.Goods;
import com.ylx.pojo.Order;
import com.ylx.pojo.OrderItem;
import com.ylx.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class OrderServiceImpl implements OrderService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public int add (Order order) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        int n = orderMapper.add(order);
        sqlSession.commit();
        sqlSession.close();
        return n;

    }

    @Override
    public void update (Order order) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        orderMapper.update(order);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public  Order selectByOid (String oid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        Order order = orderMapper.selectByOid(oid);
        order.setOrderItems(selectOrderItemOid(oid));
        sqlSession.close();
        return order;
    }

    @Override
    public void updateOrderState (String oid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        orderMapper.updateOrderState(oid);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Order getOrderByOid (String oid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        Order order = orderMapper.getOrderByOid(oid);
        sqlSession.close();
        return order;
    }

    @Override
    public List<Order> getOrderByUid (String uid, String birthYear, String birthMonth) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        List<Order> orderList = orderMapper.getOrderByUid(uid,birthYear,birthMonth);

        for (Order order : orderList) {
            List<OrderItem> orderItems = selectOrderItemOid(order.getOid());

            order.setOrderItems(orderItems);
        }

        sqlSession.close();

        return orderList;
    }

    @Override
    public void addOrderItem (OrderItem or) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        orderMapper.addOrderItem(or);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public List<OrderItem> selectOrderItemOid (String oid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);

        List<OrderItem> orderItems = orderMapper.selectOrderItemOid(oid);



//        for(int i=0;i<orderItems.size();i++){
//
//
//            Goods goods = orderMapper.getProductsById (list.get(i).getId());
//
//
//            orderItems.get(i).setGoods(goods);
//
//        }

        for (OrderItem orderItem : orderItems) {

            Goods goods = getProductsById(orderItem.getId());

            orderItem.setGoods(goods);

        }

        sqlSession.close();
        return orderItems;
    }

    @Override
    public   List<OrderItem> selectOrderItemOid1 (){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);

        List<OrderItem> orderItems = orderMapper.selectOrderItemOid1();
        for (OrderItem orderItem : orderItems) {

            Goods goods = getProductsById(orderItem.getId());

            orderItem.setGoods(goods);

        }
        sqlSession.close();
        return orderItems;
    }

    @Override
    public Goods getProductsById (String id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        Goods goods = orderMapper.getProductsById(id);
        sqlSession.close();
        return goods;
    }

    @Override
    public List<Order> getAllOrderList () {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        List<Order> list = orderMapper.getAllOrderList();
//        for (int i = 0; i < list.size(); i++) {
//
//            List<OrderItem> orderItems = selectOrderItemOid(String.valueOf(list.get(i).getOid()));
//            list.get(i).setOrderItems(orderItems);
//
//        }
        for(Order o:list){

            List<OrderItem> itemList = orderMapper.selectOrderItemOid(o.getOid());

            o.setOrderItems(itemList);
        }
        sqlSession.close();
        return  list;
    }
    @Override
    public int deleteOrderItem (String oid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        orderMapper.deleteOrderItem(oid);
        sqlSession.commit();
        sqlSession.close();
        return   deleteOrder(oid);
    }




    @Override
    public int deleteOrder (String oid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        int i = orderMapper.deleteOrder(oid);
        sqlSession.commit();
        sqlSession.close();
        return i;

    }

    @Override
    public List<Goods> getMenu () {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        List<Goods> goods = orderMapper.getMenu();
        sqlSession.close();
        return  goods;
    }

    @Override
    public List<Goods> getLisiMenu () {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        List<Goods> goods = orderMapper.getLisiMenu();
        sqlSession.close();
        return  goods;
    }




    @Override
   public List<OrderItem> selectOrderitem(){

        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        List<OrderItem> orderitems = orderMapper.selectOrderitem();
        sqlSession.close();
        return orderitems;
    }
    @Override
    public List<Order> selectUser (String NAME) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        if (NAME != null && NAME.length() > 0) {
            NAME=("%" + NAME + "%");
        }
        List<Order> orders = orderMapper.selectUser(NAME);
       sqlSession.close();
       return orders;



    }

}
